; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --function-signature
; RUN: opt -S -passes='openmp-opt' < %s | FileCheck %s
; RUN: opt -openmp-opt-disable-folding -S -passes='openmp-opt' < %s | FileCheck %s --check-prefix=CHECK-DISABLED
; ModuleID = 'single_threaded_exeuction.c'

%struct.ident_t = type { i32, i32, i32, i32, i8* }

@0 = private unnamed_addr constant [1 x i8] c"\00", align 1
@1 = private unnamed_addr constant %struct.ident_t { i32 0, i32 2, i32 0, i32 0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @0, i32 0, i32 0) }, align 8

define void @kernel() {
; CHECK-LABEL: define {{[^@]+}}@kernel() {
; CHECK-NEXT:    [[CALL:%.*]] = call i32 @__kmpc_target_init(%struct.ident_t* nonnull @[[GLOB1:[0-9]+]], i1 false, i1 false, i1 false)
; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[CALL]], -1
; CHECK-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    call void @foo()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.else:
; CHECK-NEXT:    call void @bar()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    call void @__kmpc_target_deinit(%struct.ident_t* null, i1 false, i1 true)
; CHECK-NEXT:    ret void
;
; CHECK-DISABLED-LABEL: define {{[^@]+}}@kernel() {
; CHECK-DISABLED-NEXT:    [[CALL:%.*]] = call i32 @__kmpc_target_init(%struct.ident_t* nonnull @[[GLOB1:[0-9]+]], i1 false, i1 false, i1 false)
; CHECK-DISABLED-NEXT:    [[CMP:%.*]] = icmp eq i32 [[CALL]], -1
; CHECK-DISABLED-NEXT:    br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK-DISABLED:       if.then:
; CHECK-DISABLED-NEXT:    call void @foo()
; CHECK-DISABLED-NEXT:    br label [[IF_END:%.*]]
; CHECK-DISABLED:       if.else:
; CHECK-DISABLED-NEXT:    call void @bar()
; CHECK-DISABLED-NEXT:    br label [[IF_END]]
; CHECK-DISABLED:       if.end:
; CHECK-DISABLED-NEXT:    call void @__kmpc_target_deinit(%struct.ident_t* null, i1 false, i1 true)
; CHECK-DISABLED-NEXT:    ret void
;
  %call = call i32 @__kmpc_target_init(%struct.ident_t* nonnull @1, i1 false, i1 false, i1 false)
  %cmp = icmp eq i32 %call, -1
  br i1 %cmp, label %if.then, label %if.else
if.then:
  call void @foo()
  br label %if.end
if.else:
  call void @bar()
  br label %if.end
if.end:
  call void @__kmpc_target_deinit(%struct.ident_t* null, i1 false, i1 true)
  ret void
}

; Function Attrs: noinline
define internal void @foo() {
; CHECK-LABEL: define {{[^@]+}}@foo() {
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TID:%.*]] = call i32 @__kmpc_get_hardware_thread_id()
; CHECK-NEXT:    br label [[IF_THEN:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    call void @baz()
; CHECK-NEXT:    br label [[IF_END:%.*]]
; CHECK:       if.end:
; CHECK-NEXT:    ret void
;
; CHECK-DISABLED-LABEL: define {{[^@]+}}@foo() {
; CHECK-DISABLED-NEXT:  entry:
; CHECK-DISABLED-NEXT:    [[TID:%.*]] = call i32 @__kmpc_get_hardware_thread_id()
; CHECK-DISABLED-NEXT:    [[ISMAIN:%.*]] = call signext i8 @__kmpc_is_generic_main_thread_id(i32 [[TID]])
; CHECK-DISABLED-NEXT:    [[PRED:%.*]] = icmp eq i8 [[ISMAIN]], 1
; CHECK-DISABLED-NEXT:    br i1 [[PRED]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK-DISABLED:       if.then:
; CHECK-DISABLED-NEXT:    call void @baz()
; CHECK-DISABLED-NEXT:    br label [[IF_END]]
; CHECK-DISABLED:       if.end:
; CHECK-DISABLED-NEXT:    ret void
;
entry:
  %tid = call i32 @__kmpc_get_hardware_thread_id();
  %ismain = call signext i8 @__kmpc_is_generic_main_thread_id(i32 %tid)
  %pred = icmp eq i8 %ismain, 1
  br i1 %pred, label %if.then, label %if.end

if.then:
  call void @baz()
  br label %if.end

if.end:
  ret void
}

; Function Attrs: noinline
define internal void @bar() {
; CHECK-LABEL: define {{[^@]+}}@bar() {
; CHECK-NEXT:  entry:
; CHECK-NEXT:    [[TID:%.*]] = call i32 @__kmpc_get_hardware_thread_id()
; CHECK-NEXT:    [[ISMAIN:%.*]] = call signext i8 @__kmpc_is_generic_main_thread_id(i32 [[TID]])
; CHECK-NEXT:    [[PRED:%.*]] = icmp eq i8 [[ISMAIN]], 1
; CHECK-NEXT:    br i1 [[PRED]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK:       if.then:
; CHECK-NEXT:    call void @baz()
; CHECK-NEXT:    br label [[IF_END]]
; CHECK:       if.end:
; CHECK-NEXT:    ret void
;
; CHECK-DISABLED-LABEL: define {{[^@]+}}@bar() {
; CHECK-DISABLED-NEXT:  entry:
; CHECK-DISABLED-NEXT:    [[TID:%.*]] = call i32 @__kmpc_get_hardware_thread_id()
; CHECK-DISABLED-NEXT:    [[ISMAIN:%.*]] = call signext i8 @__kmpc_is_generic_main_thread_id(i32 [[TID]])
; CHECK-DISABLED-NEXT:    [[PRED:%.*]] = icmp eq i8 [[ISMAIN]], 1
; CHECK-DISABLED-NEXT:    br i1 [[PRED]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
; CHECK-DISABLED:       if.then:
; CHECK-DISABLED-NEXT:    call void @baz()
; CHECK-DISABLED-NEXT:    br label [[IF_END]]
; CHECK-DISABLED:       if.end:
; CHECK-DISABLED-NEXT:    ret void
;
entry:
  %tid = call i32 @__kmpc_get_hardware_thread_id();
  %ismain = call signext i8 @__kmpc_is_generic_main_thread_id(i32 %tid)
  %pred = icmp eq i8 %ismain, 1
  br i1 %pred, label %if.then, label %if.end

if.then:
  call void @baz()
  br label %if.end

if.end:
  ret void
}

declare void @baz()

declare i8 @__kmpc_is_generic_main_thread_id(i32)

declare i32 @__kmpc_get_hardware_thread_id()

declare i32 @__kmpc_target_init(%struct.ident_t*, i1, i1, i1)

declare void @__kmpc_target_deinit(%struct.ident_t*, i1, i1)

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4, !5, !6}
!nvvm.annotations = !{!7}

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 13.0.0", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "fold_generic_main_thread.c", directory: "/tmp/fold_generic_main_thread.c")
!2 = !{}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !{i32 1, !"wchar_size", i32 4}
!5 = !{i32 7, !"openmp", i32 50}
!6 = !{i32 7, !"openmp-device", i32 50}
!7 = !{void ()* @kernel, !"kernel", i32 1}
